#include<iostream>

using namespace std;

const int p=1e9+7;
typedef long long ll;

ll qpow(ll a,ll b,ll c)
{
	int ret=1;
	while(b)
	{
		if(b&1)
		{
			ret=ret*a%c;
		}
		b>>=1;
		a=a*a%c;
	}
	return ret;
}

int main()
{
	int t;cin>>t;

	while(t--)
	{
		int n,k;cin>>n>>k;

		int x=qpow(k,p-2,p);
		ll ret=(qpow(1+k,n+1,p)-(1+k))*x%p;
		cout<<(ret+p)%p<<endl;
	}
}